package v1

import (
	"gin-vue-admin/global"
    "gin-vue-admin/model"
    "gin-vue-admin/model/request"
    "gin-vue-admin/model/response"
    "gin-vue-admin/service"
    "github.com/gin-gonic/gin"
    "go.uber.org/zap"
)

// @Tags SelectList
// @Summary 创建SelectList
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.SelectList true "创建SelectList"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /selectList/createSelectList [post]
func CreateSelectList(c *gin.Context) {
	var selectList model.SelectList
	_ = c.ShouldBindJSON(&selectList)
	selectList.SelectOwner = getUserName(c)
	if err := service.CreateSelectList(selectList); err != nil {
        global.GVA_LOG.Error("创建失败!", zap.Any("err", err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// @Tags SelectList
// @Summary 删除SelectList
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.SelectList true "删除SelectList"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /selectList/deleteSelectList [delete]
func DeleteSelectList(c *gin.Context) {
	var selectList model.SelectList
	_ = c.ShouldBindJSON(&selectList)
	if err := service.DeleteSelectList(selectList); err != nil {
        global.GVA_LOG.Error("删除失败!", zap.Any("err", err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// @Tags SelectList
// @Summary 批量删除SelectList
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除SelectList"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /selectList/deleteSelectListByIds [delete]
func DeleteSelectListByIds(c *gin.Context) {
	var IDS request.IdsReq
    _ = c.ShouldBindJSON(&IDS)
	if err := service.DeleteSelectListByIds(IDS); err != nil {
        global.GVA_LOG.Error("批量删除失败!", zap.Any("err", err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// @Tags SelectList
// @Summary 更新SelectList
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.SelectList true "更新SelectList"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /selectList/updateSelectList [put]
func UpdateSelectList(c *gin.Context) {
	var selectList model.SelectList
	_ = c.ShouldBindJSON(&selectList)
	if err := service.UpdateSelectList(selectList); err != nil {
        global.GVA_LOG.Error("更新失败!", zap.Any("err", err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// @Tags SelectList
// @Summary 用id查询SelectList
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.SelectList true "用id查询SelectList"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /selectList/findSelectList [get]
func FindSelectList(c *gin.Context) {
	var selectList model.SelectList
	_ = c.ShouldBindQuery(&selectList)
	if err, reselectList := service.GetSelectList(selectList.ID); err != nil {
        global.GVA_LOG.Error("查询失败!", zap.Any("err", err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"reselectList": reselectList}, c)
	}
}

// @Tags SelectList
// @Summary 分页获取SelectList列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.SelectListSearch true "分页获取SelectList列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /selectList/getSelectListList [get]
func GetSelectListList(c *gin.Context) {
	var pageInfo request.SelectListSearch
	_ = c.ShouldBindQuery(&pageInfo)
	if err, list, total := service.GetSelectListInfoList(pageInfo); err != nil {
	    global.GVA_LOG.Error("获取失败", zap.Any("err", err))
        response.FailWithMessage("获取失败", c)
    } else {
        response.OkWithDetailed(response.PageResult{
            List:     list,
            Total:    total,
            Page:     pageInfo.Page,
            PageSize: pageInfo.PageSize,
        }, "获取成功", c)
    }
}

// 赞
func Recommend(c *gin.Context) {
	var pageInfo model.SelectList
	_ = c.ShouldBindJSON(&pageInfo)
	referrer := getUserName(c)
	if err := service.RecommendGoods(pageInfo, referrer); err != nil {
		global.GVA_LOG.Error("赞失败", zap.Any("err", err))
		response.FailWithMessage("赞失败", c)
	} else {
		response.OkWithMessage("赞成功", c)
	}
}

// 踩
func Unrecommend(c *gin.Context) {
	var pageInfo model.SelectList
	_ = c.ShouldBindJSON(&pageInfo)
	referrer := getUserName(c)
	if err := service.UnrecommendGoods(pageInfo, referrer); err != nil {
		global.GVA_LOG.Error("踩失败", zap.Any("err", err))
		response.FailWithMessage("踩失败", c)
	} else {
		response.OkWithMessage("踩成功", c)
	}
}
